﻿var MyProfilePage = function(pageContainer) {

    MyProfilePage.superclass.constructor.call(this, pageContainer);

    var self = this;

    var Elements = {
        userImg: pageContainer.find(".userImg"),
        changePic: pageContainer.find(".changePic"),
        userName: pageContainer.find(".userName"),
        userFctName: pageContainer.find(".userFctName"),
        userDepName: pageContainer.find(".userDepName"),
        userCNP: pageContainer.find(".userCNP"),
        userMarca: pageContainer.find(".userMarca"),
        changePsw: pageContainer.find(".changePsw"),
        disconnect: pageContainer.find(".disconnect")
    };

    self.initialize = override(self.initialize, function() {
        self.Title = translate('Module_Title_MyProfile');
        app.translatePage(app.Pages.MyProfile);

        getUserInfo();

        Elements.changePic.tap(function() {
            app.Phone.getPicture(translate('PhoneDialog_GetPicture_Title'), translate('PhoneDialog_GetPicture_Camera'), translate('PhoneDialog_GetPicture_Gallery'), translate('PhoneDialog_GetPicture_Cancel'));
        });

        Elements.changePsw.tap(function() {
            if (!app.Pages.ChangePassword.instance)
                app.Pages.ChangePassword.instance = new ChangePasswordPage(app.Pages.ChangePassword.container);
            app.changePage(app.Pages.ChangePassword);
        });

        Elements.disconnect.tap(function() {
            app.disconnect();
        });

        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(false);
        });

        $(document).bind('changedCompany', function() {
            getUserInfo();
        });
    });

    this.savePicture = function(picture) {
        Elements.userImg.hide();
        app.TMMService.TMM.SaveUserPicture(picture.pic, function(response) {
            if (response && response.Exception) {
                Elements.userImg.show();
                app.MessagesFactory.showToastWarning(translate('General_Eroare, MyProfile_ChangePicError'));
            } else if (response && !response.Exception) {
                var src = "data:image/jpeg;base64," + picture.pic;
                Elements.userImg.show().attr("src", src);
                Event.sendEvent(null, "setUserPic", { src: src });
            } else {
                app.MessagesFactory.showToastWarning(translate('General_Eroare, MyProfile_ChangePicError'));
            };
        });
    };

    var getUserInfo = function(callback) {
        app.TMMService.TMM.GetUserInfo(function(response) {
            if (response && response.FinalResult) {
                var user = response.FinalResult;
                var userPhoto = Utils.getEmployeePhoto(user.HasPicture ? (user.PRS_ID ? user.PRS_ID : user.Username) : 0);
                Elements.userImg.attr("src", userPhoto);
                Elements.userName.html(user.Name);
                if (user.FNC_NUME != null && user.FNC_NUME.length != 0)
                    Elements.userFctName.html(user.FNC_NUME);
                if (user.DPT_NAME != null && user.DPT_NUME.length != 0)
                    Elements.userDepName.html(user.DPT_NAME);
                if (user.Marca != null && user.Marca.length != 0)
                    Elements.userMarca.html(user.Marca);
                if (user.CNP != null && user.CNP.length != 0)
                    Elements.userCNP.html(user.CNP);
                if (user.Role && user.Role.RoleType > 4)
                    if (user.Role.RoleType == Roles.DRU)
                        Elements.userFctName.html(translate('General_DRU'));
                    else if (user.Role.RoleType == Roles.MRU)
                        Elements.userFctName.html(translate('General_MRU'));
                    else if (user.Role.RoleType == Roles.OS)
                        Elements.userFctName.html(translate('General_OS'));
                if (callback)
                    callback(response);
            }
        });
    };

    self.initialize();
};
extend(MyProfilePage, TMMobilePage);